package com.example.boot.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.boot.ResultEnum.ResultEnum;
import com.example.boot.exception.BootException;
import com.example.boot.user.dao.UserInfoDao;
import com.example.boot.user.entity.UserInfoEntity;
import com.example.boot.user.service.UserInfoService;
import com.example.boot.util.NewUUID;

@Service
public class UserInfoImpl implements UserInfoService {
	@Autowired
	private UserInfoDao userInfoDao;

	@Override
	public boolean addUserInfo(UserInfoEntity user) throws Exception {
		if (userInfoDao.byNameFindUser(user.getName()) != null) {
			throw new BootException(ResultEnum.USER_ISHAVE);
		}

		user.setId(NewUUID.UUID());
		return (userInfoDao.addUserInfo(user) == 1) ? true : false;
	}

	@Override
	public UserInfoEntity userLogin(String name, String pwd) throws Exception {
		UserInfoEntity byNameFindUser = userInfoDao.byNameFindUser(name);
		if (byNameFindUser == null) {
			throw new BootException(ResultEnum.USER_HAVET);
		} else if (name.equals(byNameFindUser.getName()) && pwd.equals(byNameFindUser.getPwd())) {
			return byNameFindUser;
		} else {
			throw new BootException(ResultEnum.X_PWD);
		}

	}

}
